home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3694 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. Path: li.net!usenet
  2. From: bsilvern@li.net (Bob Silvern)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: BORLAND C++ 4.5 wont add .1 and .9
  5. Date: Thu, 25 Jan 1996 17:15:06 GMT
  6. Organization: Harmony Graphics
  7. Message-ID: <4e8dqv$ins@linet02.li.net>
  8. References: <1996Jan18.044900.10609@oasis.enmu.edu> <96019.113901CWF102@psuvm.psu.edu>
  9. NNTP-Posting-Host: lisuser44.li.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. The Mighty Stymie <CWF102@psuvm.psu.edu> wrote:
  13.  
  14. >I believe your problem has something to do with the way Borland stroes floating
  15. >point numbers.  I had a similar problem recently:
  16.  
  17. >if(x>0.1)
  18. >  do this
  19. >else
  20. >  do that
  21.  
  22. >If x=0.1 it would still do "this" and it wouldn't do "that" until x<0.1
  23. >I was only concerned with increments or decrements of 0.1, so I did this
  24.  
  25. >if(x>0.11)
  26. >  do this
  27. >else
  28. >  do that
  29.  
  30. >If you tack on a digit beyond the on your concerned with, things may work.  As
  31. >I finish this note, I forget what your problem was specifically, but I know it
  32. >was similar to this. :)
  33. >-christopher w. felter
  34.  
  35. >*******************************************************************************
  36. >* ____    __  __  ____ Christopher W. Felter  (alias: The Mighty Stymie)      *
  37. >*  ---\    / /   /---  cwf102@psuvm.psu.edu       cwf102@email.psu.edu        *
  38. >*   ---\  / /___/--    felt43@eetsg00.bd.psu.edu  stymie@psu.edu              *
  39. >*       \/ /   /       http://metro.turnpike.net/S/stymie                     *
  40. >*         /   /        "If I get home and there's sour cream on them, those   *
  41. >*            /          food, my husband's gonna be pissed.  I said pissed!"  *
  42. >*           /           - Overweight, midwestern woman at Taco Bell, Oxford,  *
  43. >*                         Ohio (September 11, 1995)                           *
  44. >*******************************************************************************
  45.  
  46. How are you assigning the value 0.1 to x?  If you are directly assinging the
  47. value (i.e. x=0.1), then I'm surprised this doesn't work.  However, if you are
  48. getting there by floating point calculations, (i.e. x=0.05; x+=x) then you must
  49. consider that floating point numbers are internally stored as binary
  50. approximations.  Even though the debugger may display the value as 0.1 rounding
  51. the display off to 8 significant digits, the presise value which is limited to
  52. the precision of your machine, in this case 4 bytes for a float, may be closer
  53. to 0.100000000000000000000000001.  Thus  x > 0.1.  This is a problem you will
  54. find when doing floating point comparisons on any platform with any compiler,
  55. not just Borland's. 
  56.  
  57.  
  58.